package com.yanq.liancar.adapter;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/11/11  10:59
 *  @描述       ${TODO}
 */


import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yanq.liancar.R;
import com.yanq.liancar.address_selection.utils.PinyinUtils;
import com.yanq.liancar.bean.StaffAdministrationBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

public class StaffAdministrationAdapter extends BaseAdapter {
    private List<StaffAdministrationBean.DataBean> mList;
    private HashMap<String, Integer> letterIndexes;
    private Context mContext;
    private OnClickListener onClickListener;

    public StaffAdministrationAdapter(Context context, List<StaffAdministrationBean.DataBean> mList) {
        this.mContext=context;
        this.mList=mList;

        if (mList == null){

            mList = new ArrayList<>();

        }
        letterIndexes = new HashMap<>();
        for (int index = 0; index < mList.size(); index++) {

            //当前城市拼音首字母

            String currentLetter = getFirstLetter(mList.get(index).getSpell());

            //上个首字母，如果不存在设为""

            String previousLetter = index >= 1 ? PinyinUtils.getFirstLetter(mList.get(index - 1).getSpell()) : "";

            if (!TextUtils.equals(currentLetter, previousLetter)) {

                letterIndexes.put(currentLetter, index);

            }

        }

    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public StaffAdministrationBean.DataBean getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        StaffViewHolder holder;
        if (view==null){
            view= LayoutInflater.from(mContext).inflate(R.layout.item_taffadministration, viewGroup, false);
            holder=new StaffViewHolder();
            holder.name=view.findViewById(R.id.tv_name);
            holder.phone=view.findViewById(R.id.tv_phone);
            holder.item=view.findViewById(R.id.item);
            view.setTag(holder);
        }else {
            holder= (StaffViewHolder) view.getTag();
        }

        holder.name.setText(mList.get(i).getUsername());
        holder.phone.setText(mList.get(i).getTel());
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              onClickListener.onCityClick(mList.get(i).getUid());
            }
        });
        return view;
    }

    public static class StaffViewHolder{

        TextView phone;

        TextView name;

        LinearLayout item;

    }
    /**
     * 获取字母索引的位置
     *
     * @param letter
     * @return
     */

    public int getLetterPosition(String letter) {

        Integer integer = letterIndexes.get(letter);

        return integer == null ? -1 : integer;

    }

    /**
     * 获取拼音的首字母（大写）
     *
     * @param pinyin
     * @return
     */

    public static String getFirstLetter(final String pinyin) {

        if (TextUtils.isEmpty(pinyin))
            return "A";

        String c = pinyin.substring(0, 1);

        Pattern pattern = Pattern.compile("^[A-Za-z]+$");

        if (pattern.matcher(c).matches()) {

            return c.toUpperCase();

        }

        return "A";

    }

    public void setOnClickListener(OnClickListener listener){

        this.onClickListener = listener;

    }



    public interface OnClickListener{

        void onCityClick(int id);

    }
}
